<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-04-08
 * Time: 11:58
 */

class ReportModel extends MY_Model{

    public $table = 't_report';

    public function __construct(){
        parent::__construct();
    }
    public $tag = array(0=>"内容噪音过大", 1 => "含垃圾或广告信息", 2 => "含低俗色情内容", 3 => "恶意抄袭", 4 => "含侮辱性词汇", 5 => "含政治敏感内容");
    public $type = array(0 => "用户", 1 => "帖子", 2 => "评论");
    public $punish = array(4 => "禁言", 5 => "禁闭", 6 => "封号");
    public $punish_time = array(1 => "30分钟", 2 => "一小时", 3 => "24小时", 4 => "7天", 5 => "永久", 6 => "解除", 7 => "3天");

    /**
     * 通过一个举报的数据，查出所有此帖子或用户或评论的内容
     * name: get_list_all
     * @param $id
     * @return array
     * @throws Exception
     * @Author:taiyang
     */
    public function get_list_all($id){
        $sql="select tp.* from t_report tp inner join t_report tpy on tp.type=tpy.type and tp.be_id=tpy.be_id where 1 and tpy.id='".$id."' and tp.status=0 ";
        $list=$this->query_sql_list($sql);
        return $list;
    }

    public function getReportList($where=array(),$page, $pagesize){
        $sql="select type,be_id from t_report where 1 ";
        if($where['status'] == 1){
            $sql.=" and status = 0 ";
        }elseif($where['status'] == 2){
            $sql.=" and status > -2 and status <> 0 ";
        }
        $sql.=" group by type,be_id ";
        $index = $page - 1 < 0 ? 0 : ($page - 1) * $pagesize;
        $sql.=" limit ".$index.",".$pagesize."";
        return $this->query_sql_list($sql);
    }

    public function getReportNum($where=array()){
        $sql='select count(*) as nums from (';

        $sql.="select type,be_id from t_report where 1 ";
        if($where['status'] == 1){
            $sql.=" and status = 0 ";
        }elseif($where['status'] == 2){
            $sql.=" and status > -2 and status <> 0 ";
        }
        $sql.=" group by type,be_id ";


        $sql.=") t";
        return $this->query_sql_count($sql);

    }

    /**
     * 查出指定数据
     * name: get_data
     * @param string $type
     * @param string $be_id
     * @param int $status 1未处理 2已经处理
     * @return array|bool
     * @throws Exception
     * @Author:taiyang
     */
    public function get_data($type='',$be_id='',$status=1){
        if((empty($type)&&$type!=0) || empty($be_id)){
            return false;
        }
        //查询出指定的数据
        $sql="select tag from ".$this->table." where 1 ";
        $sql.=" and type=".$type." and be_id=".$be_id." ";
        if($status == 1){
            $sql.=" and status = 0 ";
        }elseif($status == 2){
            $sql.="  and status > -2 and status <> 0  ";
        }
        //查出所有标签和数量
        $res=$this->query_sql_list($sql);
        $reportTag = $this->ReportModel->tag;
        $tag=array();
        $arr=array();
        $tag_num=array();
        foreach ($res as $k=>$v){
            $arr=explode(',',$v['tag']);
            $tag_num[$arr[0]]++;
            $tag_arr[]=$arr[0];
            $tag[]=$reportTag[$arr[0]];
        }

        if(empty($res)){return false;}
        if($type == 0){//查用户信息
            $user_data=$this->UserModel->query_one(array('uid'=>$be_id));
            $title="(用户uid".$be_id.")".$user_data['nickname'];
        }
        if($type == 1){//帖子
            $post_data=$this->PostModel->query_one(array('id'=>$be_id));
            $title='(帖子id：'.$be_id.")".$post_data['title'];
        }
        if($type == 2){//评论
            $comment_data=$this->CommentModel->query_one(array('id'=>$be_id));
            $title='(评论id：'.$be_id.')'.$comment_data['desc'];
        }
        //查询被举报人数
        $user_num=$this->get_filed_num($type,$be_id,$status,'DISTINCT uid');
        $num=$this->get_filed_num($type,$be_id,$status);
        //查出最大id的数据
        $report_sql="select * from ".$this->table." where 1 and type=".$type." and be_id=".$be_id.' ';
        if($status == 1){
            $report_sql.=" order by id desc ";
        }else{
            $report_sql.=" order by id asc ";
        }
        $report_data=$this->query_sql_one($report_sql);
        $res_data=array(
            'id'=>$report_data['id'],
            'status'=>$report_data['status'],
            'type'=>$type,
            'be_id'=>$be_id,
            'tag_num'=>$tag_num,
            'tag_arr'=>$tag_arr,
            'title'=>$title,
            'tag'=>implode('<br>',$tag),
            'count'=>'举报人数：'.$user_num."<br>举报数量：".$num
        );
        return $res_data;
    }

    /**
     * 取指定字段的统计数
     * name: get_filed_num
     * @param string $type
     * @param string $be_id
     * @param string $filed
     * @return int
     * @throws Exception
     * @Author:taiyang
     */
    public function get_filed_num($type='',$be_id='',$status='',$filed="*"){
        $sql="select count(".$filed.") from ".$this->table.' where type='.$type.' and be_id='.$be_id.'';
        if($status == 1){
            $sql.=" and status = 0 ";
        }elseif($status == 2){
            $sql.="  and status > -2 and status <> 0  ";
        }
        return $this->query_sql_count($sql);
    }
}